

function say() {
    alert("LC:105. 从前序与中序遍历序列构造二叉树");
}
say();

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
var buildTree = function(preorder, inorder) {
    //6:33-38

    if(inorder.length == 0){
        return null
    }

    var root = new TreeNode(preorder[0])

    var mid = inorder.indexOf(preorder[0])

    var inLeft = inorder.slice(0, mid)
    var inRight = inorder.slice(mid + 1)

    var prLeft = preorder.slice(1, mid+1)
    var prRight = preorder.slice(mid + 1)

    root.left = buildTree(prLeft, inLeft)
    root.right = buildTree(prRight, inRight)

    return root
};
